package main

import "fmt"

func maxProfit(prices []int) int {
	// write code here
	max := 0
	if len(prices) <= 1 { // 小于等于一个交易日,直接返回0
		return 0
	}
	minPrice := prices[0] // 记录最低价格
	for i := 1; i < len(prices); i++ {
		if diff := prices[i] - minPrice; diff > max { // 只需要和之前的最低价格比较即可
			max = diff
		} else if diff < 0 {
			minPrice = prices[i] // 更新最低价格
		}
	}

	return max
}

func main() {
	fmt.Println(maxProfit([]int{8, 9, 2, 5, 4, 7, 1}))
}
